package com.example.administrator.trains;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class activity_dowmload_ticket extends Activity {
    String startPlace;
    DatePickerDialog.OnDateSetListener time ;
    String type="all";

    private String zxh_tv1;
    String id;
    private String zxh_tv2;
    private String startTime;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_admin);

        final EditText edt_zxh_tv1= (EditText) findViewById(R.id.zxh_tv1);
        final EditText edt_zxh_tv2= (EditText) findViewById(R.id.zxh_tv2);
        final EditText edt_startTime =(EditText)findViewById(R.id.select_time);
        //火车的类型
        final TextView text_all = (TextView) findViewById(R.id.all);
        final TextView text_G = (TextView) findViewById(R.id.G);
        final TextView text_D = (TextView) findViewById(R.id.D);
        final TextView text_C = (TextView) findViewById(R.id.C);
        final TextView text_Z = (TextView) findViewById(R.id.Z);
        final TextView text_T = (TextView) findViewById(R.id.T);
        final TextView text_K = (TextView) findViewById(R.id.K);

        final Button selectButton = (Button)findViewById(R.id.selectButton);


        RelativeLayout tv3= (RelativeLayout) findViewById(R.id.tv3);

        //日期选择弹窗的日期格式处理
        time = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                monthOfYear  = monthOfYear + 1;
                if (monthOfYear < 10) {
                    if (dayOfMonth < 10) {
                        edt_startTime.setText(year+"-" + "0"+monthOfYear + "-" +"0"+ dayOfMonth );
                    } else {
                        edt_startTime.setText(year+"-" + "0"+monthOfYear + "-" + dayOfMonth );
                    }
                } else {
                    if (dayOfMonth < 10) {
                        edt_startTime.setText(year+"-"+monthOfYear + "-" +"0"+ dayOfMonth );
                    } else {
                        edt_startTime.setText(year+"-"+monthOfYear + "-"+ dayOfMonth );
                    }
                }
            }
        };


        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取日历的一个实例，里面包含了当前的年月日
                Calendar calendar=Calendar.getInstance();
                //构建一个日期对话框，该对话框已经集成了日期选择器
                //DatePickerDialog的第二个构造参数指定了日期监听器
                DatePickerDialog dialog=new DatePickerDialog(activity_dowmload_ticket.this,time,
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));

                //把日期对话框显示在界面上
                dialog.show();
            }
        });

        //设置车票座位类型的显示
        text_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select1);
                text_all.setTextColor(getResources().getColor(R.color.white));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.black));
                text_K.setTextColor(getResources().getColor(R.color.black));

                type = "all";
            }
        });
        text_G.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select1);
                text_K.setTextColor(getResources().getColor(R.color.black));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.white));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.black));
                type="G";
            }
        });
        text_D.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select1);
                text_K.setTextColor(getResources().getColor(R.color.black));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.white));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.black));
                type="D";
            }
        });
        text_C.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select1);
                text_K.setTextColor(getResources().getColor(R.color.black));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.white));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.black));
                type="C";
            }
        });
        text_Z.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select1);
                text_K.setTextColor(getResources().getColor(R.color.black));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.white));
                text_T.setTextColor(getResources().getColor(R.color.black));

                type="Z";
            }
        });
        text_T.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select);
                text_K.setBackgroundResource(R.drawable.select1);
                text_K.setTextColor(getResources().getColor(R.color.black));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.white));

                type="T";
            }
        });

        text_K.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_all.setBackgroundResource(R.drawable.select1);
                text_G.setBackgroundResource(R.drawable.select1);
                text_D.setBackgroundResource(R.drawable.select1);
                text_C.setBackgroundResource(R.drawable.select1);
                text_Z.setBackgroundResource(R.drawable.select1);
                text_T.setBackgroundResource(R.drawable.select1);
                text_K.setBackgroundResource(R.drawable.select);
                text_K.setTextColor(getResources().getColor(R.color.white));
                text_all.setTextColor(getResources().getColor(R.color.black));
                text_G.setTextColor(getResources().getColor(R.color.black));
                text_D.setTextColor(getResources().getColor(R.color.black));
                text_C.setTextColor(getResources().getColor(R.color.black));
                text_Z.setTextColor(getResources().getColor(R.color.black));
                text_T.setTextColor(getResources().getColor(R.color.black));

                type="K";
            }
        });

        selectButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.selectbutton2));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.selectbutton));

                    if (edt_zxh_tv1.getText().toString().matches("[\\u4E00-\\u9FA5]{2,5}")) {
                        if (edt_zxh_tv2.getText().toString().matches("[\\u4E00-\\u9FA5]{2,5}")) {
                            if (edt_startTime.getText().toString().matches("(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)")) {

                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                Date now_date = new Date(System.currentTimeMillis());
                                String string_now_date = simpleDateFormat.format(now_date);
                                String[] temp = string_now_date.split("-");
                                int now_year = Integer.parseInt(temp[0]);
                                int now_month = Integer.parseInt(temp[1]);
                                int now_day = Integer.parseInt(temp[2]);

                                String require_date = edt_startTime.getText().toString();
                                temp = require_date.split("-");
                                int require_year = Integer.parseInt(temp[0]);
                                int require_month = Integer.parseInt(temp[1]);
                                int require_day = Integer.parseInt(temp[2]);
                                if (require_year < now_year) {
                                    Toast.makeText(activity_dowmload_ticket.this, "不要选择之前的日期", Toast.LENGTH_LONG).show();
                                    return false;
                                }
                                if (require_year == now_year) {
                                    if (require_month < now_month) {
                                        Toast.makeText(activity_dowmload_ticket.this, "不要选择之前的日期", Toast.LENGTH_LONG).show();
                                        return false;
                                    }
                                    if (require_month == now_month) {
                                        if (require_day < now_day) {
                                            Toast.makeText(activity_dowmload_ticket.this, "不要选择之前的日期", Toast.LENGTH_LONG).show();
                                            return false;
                                        }
                                    }
                                }
                            zxh_tv1 = edt_zxh_tv1.getText().toString();
                            zxh_tv2 = edt_zxh_tv2.getText().toString();
                            startTime = edt_startTime.getText().toString();
                            } else {
                                Toast.makeText(activity_dowmload_ticket.this, "日期格式不对", Toast.LENGTH_LONG).show();
                                return false;
                            }
                        } else {
                            Toast.makeText(activity_dowmload_ticket.this, "目的地格式不对，请不要输入中文以外的字符,要求为二到五个中文", Toast.LENGTH_LONG).show();
                            return false;
                        }
                    } else {
                        Toast.makeText(activity_dowmload_ticket.this, "出发地格式不对，请不要输入中文以外的字符,要求为二到五个中文", Toast.LENGTH_LONG).show();
                        return false;
                    }
                    if (Socket.mClient != null) {
                        try {
                            //发送请求
                            Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                    Socket.connector.FIND_TICKET, "00", zxh_tv1 + CommunicationData.empty +
                                            zxh_tv2 + CommunicationData.empty +
                                            startTime + CommunicationData.empty));
                            /*接收服务端是否成功接收信息的ack*/
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == "01") {
                                Intent i = new Intent(activity_dowmload_ticket.this, admin_show.class);
                                i.putExtra("type", type);
                                i.putExtra("date", startTime);
                                startActivity(i);
                            } else {
                                Toast.makeText(activity_dowmload_ticket.this, "服务器没有响应，请稍后搜索", Toast.LENGTH_LONG).show();
                                Socket.connector.releaseSocket();
                                return false;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(activity_dowmload_ticket.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                        Socket.connector.releaseSocket();
                    }
                }
                return true;
            }
        });
    }
}
